<?php
//namespace Login\Controller;

require_once '\Controllers\IController.php';
require_once '\Views\RegisterView.php';
require_once '\Models\RegisterModel.php';
require_once '\Helpers\Validate.php';

class RegisterController implements IController{
	
	private $m_registerModel;
	
	public function __construct(RegisterModel $registerModel){
			$this->m_registerModel = $registerModel;
	}
	
	public function CreateController()
	{		
		$registerView = new RegisterView();
		//$registerModel = new RegisterModel();
		$validate = new Validate();
		
		$html = $registerView->CreateRegisterBox();
		
		//Försöker registrera
		if($registerView->TriedToRegister()){
			
			//Hämta indata	
			$desiredUserName = $registerView->GetDesiredUserName();
			$desiredPassword = $registerView->GetDesiredPassword();
			$confirmedPassword = $registerView->GetConfirmedPassword();
			
			//Kontrollera att input innehåller tillåtna tecken
			if($validate->IsValidUsername($desiredUserName) && $validate->IsValidPassword($desiredPassword)){
				
				//Kontrollera så att lösenorden stämmer överens	
				if($desiredPassword == $confirmedPassword){
					if($this->m_registerModel->Register($desiredUserName, sha1($desiredPassword))){
						$html = $registerView->SuccessedRegisterBox();
					}
					else {
						$html = $registerView->UserExists();
						$html .= $registerView->CreateRegisterBox();
					}			
				}
				else {
					$html = $registerView->InvalidConfirmedPasswordBox();
					$html .= $registerView->CreateRegisterBox();					
				}
			}
			else {
				$html = $registerView->InvalidCharacters();
				$html .= $registerView->CreateRegisterBox();
			}
		}
		return $html;
	}
	
}